package org.vehicle.service.security;

import java.util.ArrayList;
import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.vehicle.common.MyConstant;
import org.vehicle.dao.ManagerInfoMapper;
import org.vehicle.dao.OrganizationInfoMapper;
import org.vehicle.entity.ManagerInfo;
import org.vehicle.entity.OrganizationInfo;
import org.vehicle.entity.security.MyUserDetail;

import com.fasterxml.jackson.databind.ser.std.StdKeySerializers.Default;
@Service
public class UserDetailsServiceImpl implements UserDetailsService{
	@Autowired
	private ManagerInfoMapper managerInfoMapper;
	@Autowired
	private OrganizationInfoMapper organizationInfoMapper;
	@Override
	public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
		ManagerInfo managerInfo=managerInfoMapper.findManagerInfoByLoginName(userName);
		if(managerInfo == null){ 
            throw new UsernameNotFoundException("用户名不存在");
        }
		MyUserDetail userDetail=null;
		OrganizationInfo organizationInfo=organizationInfoMapper.selectByPrimaryKey(managerInfo.getOrganizationInfoId());
		if(managerInfo.getManagerType()==null) {
			throw new UsernameNotFoundException("用户状态不可用");
		}
		switch (managerInfo.getManagerType()) {
		case MyConstant.MANAGER_TYPE_ROOT:
			userDetail=new MyUserDetail(managerInfo.getLoginName(), managerInfo.getPassword(), getAuthorities("ROLE_ROOT"));
			break;
		case MyConstant.MANAGER_TYPE_ADMIN:
			userDetail=new MyUserDetail(managerInfo.getLoginName(), managerInfo.getPassword(), getAuthorities("ROLE_ADMIN"));
			break;
		default: throw new UsernameNotFoundException("用户状态不可用");
		}
		userDetail.setManagerInfoId(managerInfo.getManagerInfoId());
		userDetail.setManagerType(managerInfo.getManagerType());
		userDetail.setOrganizationInfoId(organizationInfo.getOrganizationInfoId());
		userDetail.setOrganizationName(organizationInfo.getOrganizationName());
		userDetail.setOrganizationType(organizationInfo.getOrganizationType());
		userDetail.setManagerName(managerInfo.getManagerName());
		userDetail.setGender(managerInfo.getGender());
		userDetail.setJobNumber(managerInfo.getJobNumber());
		userDetail.setMobilePhone(managerInfo.getMobilePhone());
		userDetail.setOfficePhone(managerInfo.getOfficePhone());
		return userDetail;
	}
	private Collection<GrantedAuthority> getAuthorities(String role) {
        Collection<GrantedAuthority> grantedAuthorities = new ArrayList<GrantedAuthority>();
        SimpleGrantedAuthority grantedAuthority = new SimpleGrantedAuthority(role);
        grantedAuthorities.add(grantedAuthority);
        return grantedAuthorities;
    }
	
}
